export const formatFormObject = (item) => {
  const result = {
    psnId: item.psnId,
    id: item.id
  };

  const list = JSON.parse(item.formJson);

  if (!list || !Array.isArray(list)) return result;

  list.forEach(item => {
    // 如果是手机号字段则加上区号
    if (item.attribute.filedCode === 'uTel') {
      result[item.attribute.filedCode] = `${item.attribute.areaCode||86}-${item.attribute.filedValue}`;
    } else {
      result[item.attribute.filedCode] = item.attribute.filedValue;
    }
  });

  // 增加一个字段保存表单数据
  result.formJson = list;

  return result;
};

export const addZero = (value) => value > 9 ? String(value) : `0${value}`;

export const getURLParameter = (parameter) => {
  // eslint-disable-next-line no-sparse-arrays
  return decodeURIComponent((new RegExp('[?|&]' + parameter + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || '';
}
